from glob import escape
import os, shutil, io, sys
import os.path as osp
import json, time
from importlib_metadata import files
import numpy as np
import cv2
import copy
import base64, requests
from multiprocessing import Pool
from tqdm import tqdm
from PIL import Image
from tool import filesystem, via_tool, darknet_tool, opencv_tool # export PYTHONPATH=$PYTHONPATH:`pwd`

def move_img_to_train(data_dir, dest_dir):
    last_dirs = filesystem.get_last_dir(data_dir)
    for last_dir in last_dirs:
        files = filesystem.get_all_filepath(last_dir, [".jpg"])
        if len(files) == 0:
            continue
        
        for f in files:
            cur_save_dir = osp.dirname(f.replace(data_dir, dest_dir))
            os.makedirs(cur_save_dir, exist_ok=True)
            shutil.copy(f, osp.join(cur_save_dir, osp.basename(f)))


if __name__ == "__main__":
    # data_dir = "/mnt/data/sj/dataset/video/miaozhai/2022/11"
    # dest_dir = "/home/xc/work/code/paddle/train_data/det/fire/images/2022/11"
    # move_img_to_train(data_dir, dest_dir)

    # data_dir = "/home/xc/work/code/paddle/train_data/det/fire/images/2022/11"
    # opencv_tool.remove_image_by_mean(data_dir, 1, 0)



    # data_root = "/home/xc/work/code/paddle/train_data/det/fire"
    # via_name="via_region_data.big.json" 
    # gen_type="fire"
    # darknet_tool.deal_many_dir_local(data_root, via_name, gen_type)
    # darknet_tool.create_train_val_txt(data_root)
    # darknet_tool.check_darket_train_data(data_root)

    data_root = "/home/xc/work/code/paddle/train_data/det/falan"
    via_name="via_region_data.json" 
    gen_type="falan"
    darknet_tool.deal_many_dir_local(data_root, via_name, gen_type)
    darknet_tool.create_train_val_txt(data_root)
    darknet_tool.check_darket_train_data(data_root)
